package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/24 10:52
 * @description：https://leetcode-cn.com/problems/maximum-number-of-eaten-apples/
 */
public class 吃苹果的最大数目 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(
                solution.eatenApples(new int[]{3, 0, 0, 0, 0, 2}, new int[]{3, 0, 0, 0, 0, 2}));
    }

    class Solution {
        public int eatenApples(int[] apples, int[] days) {
            PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> o[1]));
            int count = 0;
            int day = 0;
            while (true) {
                if (queue.isEmpty() && day > apples.length) break;
                if (day < apples.length && apples[day] != 0) {
                    queue.offer(new int[]{apples[day], days[day] + day});
                }
                while (!queue.isEmpty() && queue.peek()[1] <= day) {
                    queue.poll();
                }
                if(!queue.isEmpty()) {
                    int[] cur = queue.peek();
                    if (--cur[0] == 0) {
                        queue.poll();
                    }
                    count++;
                }
                day++;
            }
            return count;
        }
    }
}
